//获取应用实例
const app = getApp()
Page({
  // 页面的初始数据
  data: {
    top : [],
    bottom : [],
    bannerHeight: 460,
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    indicatorColor: 'rgba(255,255,255,.3)',
    indicatorActiveColor: '#09bb07',
    hasBind : false,
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    wx.setNavigationBarTitle({ title: '首页' });
    let request = decodeURIComponent(options.q)
    if (request && request.startsWith(app.globalData.PROFILE_RECOVER_FLAG) && request > app.globalData.PROFILE_RECOVER_FLAG + 6) {
      let code = request.substring(app.globalData.PROFILE_RECOVER_FLAG.length + 6)
      app.GET('api/user/code', { code: code }, function (user) {
        if (user && user.data && user.data.id) {
          let u = user.data
          wx.navigateTo({
            url: `/pages/home/scanDetail?province=${u.province}&carnumber=${u.carnumber}&id=${u.id}&code=${u.code}`
          })
        } else {
          wx.navigateTo({ url: '/pages/home/bindCarNumber?code=' + code })
        }
      })
    }
  },
  onShow : function(){
    let _this = this;
    app.getUserInfo(function (user) {
      if (user.carnumber != null && user.code != null) {//已经绑定
        _this.setData({ hasBind: true })
      }
    });
    app.GET('api/amber/index', {}, function(res){
      let top = []
      let bottom = []
      res.data.forEach(d=>{
        if(d.location == 1){
          top.push(d)
        }else if (d.location == 2) {
          bottom.push(d)
        }
      })
      _this.setData({ top: top, bottom: bottom })
    })
  },
  bindCarNumber : function(){
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        if (res.result && res.result.startsWith(app.globalData.PROFILE_RECOVER_FLAG) &&
          res.result > app.globalData.PROFILE_RECOVER_FLAG + 6) {
          let code = res.result.substring(app.globalData.PROFILE_RECOVER_FLAG.length + 6)
          app.GET('api/user/code', {
            code: code
          }, function (user) {
            if (user && user.data && user.data.id) {
              app.showError('当前二维码已经被绑定')
            } else {
              wx.navigateTo({ url: '/pages/home/bindCarNumber?code=' + code })
            }
          })
        } else {
          app.showError('您扫描的二维码似乎不是本公司的产品')
        }
      }
    })
  },
  scanCartNumber : function(){
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        if (res.result && res.result.startsWith(app.globalData.PROFILE_RECOVER_FLAG) &&
          res.result > app.globalData.PROFILE_RECOVER_FLAG + 6) {
          let code = res.result.substring(app.globalData.PROFILE_RECOVER_FLAG.length + 6)
          app.GET('api/user/code', {
            code: code
          }, function (user) {
            if (user && user.data && user.data.id) {
              let u = user.data
              wx.navigateTo({
                url: `/pages/home/scanDetail?province=${u.province}&carnumber=${u.carnumber}&id=${u.id}&code=${u.code}`
              })
            } else {
              app.showError('当前二维码暂未被绑定')
            }
          })
        } else {
          app.showError('您扫描的二维码似乎不是本公司的产品')
        }
      }
    })
  },
  // 用户点击右上角分享
  onShareAppMessage: function() {

  }
})